import os
from FileConfigurator import FileConfigurator
from BuildWizException import BuildWizException

class Preferences:
    def __init__( self):
        self.filePath = "./config/preferences.cfg"
        self.__about = {}
        if not os.path.isfile( self.filePath):
            self.defaults = "[about]\n" \
                            "name: Build Wizard\n" \
                            "company: TQTVD\n" \
                            "version: 0.1\n" \
                            "author: Alexander T. Miro\n" \
                            "email: alexander.miro@gmail.com\n\n" \
                            "[buildmode]\n" \
                            "multibuilds: no \n\n" 
            self.__createDefaultConfigFile( self.defaults)

        try:
            self.config = FileConfigurator( self.filePath)
        except BuildWizException, e:
            print "Ooops, ", e

    def __createDefaultConfigFile( self, values):
        """ Generate a config file wich contains basic sections"""
        try:
            defFile = file( self.filePath, "w")
            defFile.write( values)
            defFile.close()
        except IOError, error:
            raise BuildWizException( "error while create defauld preferences file [%s] : " \
                  "[%s]" % (self.filePath, error))

    def getGUIFile( self, key):
        f = self.config.getKeyValue( "gui", key)
        guiDir = self.config.getKeyValue( "gui", "dir")
        return os.path.join( guiDir, f)

    def getPlatformDir( self):
        return self.config.getKeyValue( "platform", "dir")

    def getFileOfCMakeVars( self):
        cmakedir = self.config.getKeyValue( "cmake_vars_file", "dir")
        filename = self.config.getKeyValue( "cmake_vars_file", "filename")
        return os.path.join( cmakedir, filename)
                                           
    def getLogDir( self):
        logdir = self.config.getKeyValue( "log", "dir")
        return logdir

    def getDateTimeFormat4Logs( self):
        format = self.config.getKeyValue( "datetime_format", "4logs")
        return format

    def getDateTimeFormat4Humans( self):
        format = self.config.getKeyValue( "datetime_format", "4humans")
        return format

    def getAboutInfo( self):
        self.__about[ "name"] = self.config.getKeyValue( "about", "name")
        self.__about[ "copyright"] = self.config.getKeyValue( "about", "copyright")
        self.__about[ "version"] = self.config.getKeyValue( "about", "version")
        self.__about[ "author"] = self.config.getKeyValue( "about", "author")
        self.__about[ "email"] = self.config.getKeyValue( "about", "email")
        return self.__about

